clear all;
matlabpool(12);
%Setting Options
optionsLoose = optimset('TolX', 1e-5, 'TolFun',1e-5, 'MaxFunEvals', 1e5, 'MaxIter', 1e5);
optionsTight = optimset('TolX', 1e-8, 'TolFun',1e-8, 'MaxFunEvals', 1e8, 'MaxIter', 1e8);

UtilFuncParam = 1;
ChoiceProbForm = 1;
NumBootReps = 250;
RandomizationSeed = 12345;

%Loading Data
DATA = csvread('CCES_Matlab_Senate.csv', 1, 0);

VoteDem = DATA(:,1);
VoteDem_1 = DATA(:,1);
VoterIdeal = DATA(:,2);
RepPartyIdeal = DATA(:,3);
DemPartyIdeal = DATA(:,4);
RepCandIdeal = DATA(:,5);
DemCandIdeal = DATA(:,6);
IncRep = DATA(:,7);
IncDem = DATA(:,8);
ExpChallRep = DATA(:,9);
ExpChallDem = DATA(:,10);
Year2008 = DATA(:,11);
Year2010 = DATA(:,12);
Year2012 = DATA(:,13);
Age = DATA(:,14);
State_FE=DATA(:,15:63);
State_Index = DATA(:,65); 
Weights = DATA(:,66);

Time_Trend = zeros(length(VoteDem), 1);
Time_Trend(Year2008==0 & Year2010==0 & Year2012==0)= 1;
Time_Trend(Year2008==1)= 2;
Time_Trend(Year2010==1)= 3;
Time_Trend(Year2012==1)= 4;



CandTypeDem = horzcat(IncDem, ExpChallDem);
CandTypeRep = horzcat(IncRep, ExpChallRep);
clear DATA;


Lambdas = vertcat(0.5, 0.5);
RHS = [Year2008, Year2010, Year2012];
Betas0 = vertcat(1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);

[thetaTightProbit1G, fvalProbit1G] = fminunc(@(Theta) IndivVoterObjFun_Senate_Weight(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, Weights), Theta_0, optionsTight);

[SEsBoot1, thetaBoot1, exitflagBoot1] = getVarCovBootstrapCluster_Senate_Weight(thetaTightProbit1G, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, State_Index, Weights);

%%
[IncAdv_Mat_Probit1] = getIncAdv_Senate_Weight(thetaTightProbit1G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,State_Index, Weights);
csvwrite('thetaTightProbit1_Senate_Weight.csv', thetaTightProbit1G);
csvwrite('fvalProbit1_Senate_Weight.csv', fvalProbit1G);
csvwrite('IncAdv_Mat_Probit1_Senate_Weight.csv', IncAdv_Mat_Probit1);
csvwrite('thetaBoot_Probit1_Senate_Weight.csv', thetaBoot1);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit1_Boot(:,b)] = getIncAdv_Senate_Weight(thetaBoot1(:,b),VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,State_Index, Weights);
end

IncAdv_Mat_Probit1_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit1_Boot - repmat(mean(IncAdv_Mat_Probit1_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('IncAdv_Mat_Probit1_Boot_Senate_Weight.csv', IncAdv_Mat_Probit1_Boot);
csvwrite('IncAdv_Mat_Probit1_SE_Senate_Weight.csv', IncAdv_Mat_Probit1_SE);


%%
RHS = [Year2008, Year2010, Year2012, State_FE];
Betas0 = vertcat(1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit2G, fvalProbit2G] = fminunc(@(Theta) IndivVoterObjFun_Senate_Weight(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, Weights), Theta_0, optionsTight);
[SEsBoot2 thetaBoot2 exitflagBoot2] = getVarCovBootstrapCluster_Senate_Weight(thetaTightProbit2G, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, State_Index, Weights);

[IncAdv_Mat_Probit2] = getIncAdv_Senate_Weight(thetaTightProbit2G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,State_Index, Weights);
csvwrite('thetaTightProbit2_Senate_Weight.csv', thetaTightProbit2G);
csvwrite('fvalProbit2_Senate_Weight.csv', fvalProbit2G);
csvwrite('IncAdv_Mat_Probit2_Senate_Weight.csv', IncAdv_Mat_Probit2);
csvwrite('thetaBoot_Probit2_Senate_Weight.csv', thetaBoot2);


for b = 1:NumBootReps
    [IncAdv_Mat_Probit2_Boot(:,b)] = getIncAdv_Senate_Weight(thetaBoot2(:,b),VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,State_Index, Weights);
end

IncAdv_Mat_Probit2_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit2_Boot - repmat(mean(IncAdv_Mat_Probit2_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('IncAdv_Mat_Probit2_Boot_Senate_Weight.csv', IncAdv_Mat_Probit2_Boot);
csvwrite('IncAdv_Mat_Probit2_SE_Senate_Weight.csv', IncAdv_Mat_Probit2_SE);


%%

DATA = csvread('CCES_Matlab_IndivCovariates_Senate.csv', 1, 0);

VoteDem = DATA(:,1);
VoterIdeal = DATA(:,2);
RepPartyIdeal = DATA(:,3);
DemPartyIdeal = DATA(:,4);
RepCandIdeal = DATA(:,5);
DemCandIdeal = DATA(:,6);
IncRep = DATA(:,7);
IncDem = DATA(:,8);
ExpChallRep = DATA(:,9);
ExpChallDem = DATA(:,10);
Year2008 = DATA(:,11);
Year2010 = DATA(:,12);
Year2012 = DATA(:,13);
Age = DATA(:,14);
State_FE=DATA(:,15:63);
State_Index = DATA(:,65); 

HighSchool = DATA(:,66);
TwoYearCollege = DATA(:,67);
FourYearCollege = DATA(:,68);
PostBA = DATA(:,69);
Female = DATA(:,70);
Af_Am = DATA(:,71);
Latino = DATA(:,72);
Asian = DATA(:,73);
AttendRelServiceWeek = DATA(:,74);
Income = DATA(:,75);
Weights = DATA(:,76);

Time_Trend = zeros(length(VoteDem), 1);
Time_Trend(Year2008==0 & Year2010==0 & Year2012==0)= 1;
Time_Trend(Year2008==1)= 2;
Time_Trend(Year2010==1)= 3;
Time_Trend(Year2012==1)= 4;

CandTypeDem = horzcat(IncDem, ExpChallDem);
CandTypeRep = horzcat(IncRep, ExpChallRep);

%%
RHS = [Year2008, Year2010, Year2012, State_FE, HighSchool, TwoYearCollege, FourYearCollege, PostBA, Female, Af_Am, Latino, Asian, AttendRelServiceWeek, Income, Age];


Betas0 = vertcat(1, 1, zeros(length(RHS(1,:)), 1)); Theta_0 = vertcat(Lambdas, Betas0, 1);
[thetaTightProbit4G, fvalProbit4G] = fminunc(@(Theta) IndivVoterObjFun_Senate_Weight(Theta,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, Weights), Theta_0, optionsTight);
[SEsBoot4, thetaBoot4, exitflagBoot4] = getVarCovBootstrapCluster_Senate_Weight(thetaTightProbit4G, VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, ChoiceProbForm, NumBootReps, optionsLoose, optionsTight, RandomizationSeed, State_Index, Weights);

[IncAdv_Mat_Probit4] = getIncAdv_Senate_Weight(thetaTightProbit4G,VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,State_Index, Weights);


%%
csvwrite('thetaTightProbit4_Senate_Weight.csv', thetaTightProbit4G);
csvwrite('fvalProbit4_Senate_Weight.csv', fvalProbit4G);
csvwrite('IncAdv_Mat_Probit4_Senate_Weight.csv', IncAdv_Mat_Probit4);
csvwrite('thetaBoot_Probit4_Senate_Weight.csv', thetaBoot4);

for b = 1:NumBootReps
    [IncAdv_Mat_Probit4_Boot(:,b)] = getIncAdv_Senate_Weight(thetaBoot4(:,b),VoteDem, VoterIdeal, CandTypeDem, CandTypeRep, DemCandIdeal, DemPartyIdeal, RepCandIdeal, RepPartyIdeal, RHS, UtilFuncParam, 1,State_Index, Weights);
end

IncAdv_Mat_Probit4_SE = sqrt((1/(NumBootReps-1)).*sum((IncAdv_Mat_Probit4_Boot - repmat(mean(IncAdv_Mat_Probit4_Boot,2), 1, NumBootReps)).^2,2));

csvwrite('IncAdv_Mat_Probit4_Boot_Senate_Weight.csv', IncAdv_Mat_Probit4_Boot);
csvwrite('IncAdv_Mat_Probit4_SE_Senate_Weight.csv', IncAdv_Mat_Probit4_SE);

